import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 商品类型：1.手机，2.耳机
const state = {
  // 选择的商品
  selectedItem: {},
  // 流行商品
  popularGoodList: [
    {
      showSrc: '../static/images/popularGood1.jpg',
      src: '../static/images/popularGoodItem1.jpg',
      title: '魅族 16 X',
      description: '骁龙710 轻奢旗舰',
      price: '¥2398.00',
      rateValue: 4.9,
      type: 1
    },
    {
      showSrc: '../static/images/popularGood2.jpg',
      src: '../static/images/popularGoodItem2.jpg',
      title: '魅族 16th Plus',
      description: 'AI双摄光学防抖',
      price: '¥3198.00',
      rateValue: 4.1,
      type: 1
    },
    {
      showSrc: '../static/images/popularGood3.jpg',
      src: '../static/images/popularGoodItem3.jpg',
      title: '魅族 Note8',
      description: '国民拍照手机',
      price: '¥1298.00',
      rateValue: 4.5,
      type: 1
    },
    {
      showSrc: '../static/images/popularGood4.jpg',
      src: '../static/images/popularGoodItem4.jpg',
      title: '魅族 X8',
      description: '准旗舰游戏拍照手机',
      price: '¥1697.00',
      rateValue: 4.5,
      type: 1
    },
    {
      showSrc: '../static/images/popularGood5.jpg',
      src: '../static/images/popularGoodItem5.jpg',
      title: '魅族 EP52 Lite 蓝牙耳机',
      description: '超高性价比创造无线新声代',
      price: '¥129.00',
      rateValue: 4.5,
      type: 2
    }
  ],
  // 手机
  homeGoodList: [
    {
      src: '../static/images/homeGood1.png',
      title: '魅族 X8',
      description: '旗舰游戏拍照',
      price: '¥1598.00',
      rateValue: 4.9,
      type: 1
    },
    {
      src: '../static/images/homeGood2.png',
      title: '魅族 V8 高配版',
      description: '1200万+500万高清双摄',
      price: '¥1098.00',
      rateValue: 4.8,
      type: 1
    },
    {
      src: '../static/images/homeGood3.png',
      title: '魅族 15',
      description: '200万暗光双摄',
      price: '¥1998.00',
      rateValue: 4.8,
      type: 1
    },
    {
      src: '../static/images/homeGood4.png',
      title: '魅蓝 E3',
      description: '晓龙636处理器',
      price: '¥1789.00',
      rateValue: 4.5,
      type: 1
    },
    {
      src: '../static/images/homeGood5.png',
      title: '魅族 16th Plus',
      description: 'AI双摄光学防抖',
      price: '¥3198.00',
      rateValue: 4.1,
      type: 1
    },
    {
      src: '../static/images/homeGood6.png',
      title: '魅族 V8 标配版',
      description: '5.7英寸全面屏',
      price: '¥799.00',
      rateValue: 2.8,
      type: 1
    },
    {
      src: '../static/images/homeGood7.png',
      title: '魅族 M15',
      description: '2000万AI智能美颜',
      price: '¥1398.00',
      rateValue: 3.8,
      type: 1
    },
    {
      src: '../static/images/homeGood8.png',
      title: '魅蓝 6T',
      description: '双摄全面屏 游戏长续航',
      price: '¥799.00',
      rateValue: 3.5,
      type: 1
    }
  ],
  // 耳机
  headsetList: [
    {
      src: '../static/images/headset1.jpg',
      title: '魅族 HALO 激光蓝牙耳机',
      description: '12期免息 选分期免利息 日消费也就3.01',
      price: '¥999.00',
      rateValue: 4.9,
      type: 2
    },
    {
      src: '../static/images/headset2.jpg',
      title: 'POP 真无线蓝牙耳机',
      description: '六期免息 双无线 零设限',
      price: '¥499.00',
      rateValue: 4.8,
      type: 2
    },
    {
      src: '../static/images/headset3.jpg',
      title: '魅蓝 EP52 蓝牙耳机',
      description: '轻盈悦耳 动无拘束',
      price: '¥249.00',
      rateValue: 4.8,
      type: 2
    },
    {
      src: '../static/images/headset4.jpg',
      title: '魅族 EP52 Lite 蓝牙耳机',
      description: '超高性价比创造无线新声代',
      price: '¥129.00',
      rateValue: 4.5,
      type: 2
    },
    {
      src: '../static/images/headset5.jpg',
      title: '魅族 EP2X 耳机',
      description: '契合声音的流动之美',
      price: '¥69.00',
      rateValue: 4.1,
      type: 2
    },
    {
      src: '../static/images/headset6.jpg',
      title: '魅族 EP51 蓝牙运动耳机',
      description: '臻享音质 自在运动',
      price: '¥169.00',
      rateValue: 2.8,
      type: 2
    },
    {
      src: '../static/images/headset7.jpg',
      title: '魅族蓝牙音频接收器',
      description: '独立音频芯片音质媲美有线',
      price: '¥89.00',
      rateValue: 3.8,
      type: 2
    },
    {
      src: '../static/images/headset8.jpg',
      title: 'JBL T120A立体声入耳式耳机',
      description: 'Pure Bass声音 随时随地置身演唱会之中',
      price: '¥89.00',
      rateValue: 3.5,
      type: 2
    }
  ]
}
const mutations = {
  // 选择的商品
  CHANGE_SELECTED_ITEM (state, item) {
    state.selectedItem = mutations.clone(item)
    // var temp = mutations.clone(state.selectedItem)
    // sessionStorage.setItem('storageItem',JSON.stringify(temp))
  },
  // 复制对象
  clone (myObj) {
    if (typeof (myObj) !== 'object') return myObj
    if (myObj == null) return myObj
    const myNewObj = {}
    for (let i in myObj) {
      myNewObj[i] = mutations.clone(myObj[i])
    }
    return myNewObj
  }
}
const actions = {
  // 选择商品
  changeSelectedItem ({commit}, item) {
    commit('CHANGE_SELECTED_ITEM', item)
  }
}
const getters = {
  // 获取跑马灯流行商品
  getPopularGoods: function (state) {
    return state.popularGoodList
  },
  // 获取首页商品列表
  getHomeGoods: function (state) {
    return state.homeGoodList
  },
  // 获取选中的商品
  getSelectedGoods: function (state) {
    return state.selectedItem
  },
  // 获取耳机列表
  getHeadsetList: function (state) {
    return state.headsetList
  }
}

export default new Vuex.Store({
  state,
  mutations,
  actions,
  getters
})
